package cn.hengzhu.main_manager.web.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.hengzhu.main_manager.repository.dao.ManagePermissionMapper;
import cn.hengzhu.main_manager.repository.dao.ManageRolesMapper;
import cn.hengzhu.main_manager.repository.dao.ManageUserMapper;
import cn.hengzhu.main_manager.repository.domain.ManagePermission;
import cn.hengzhu.main_manager.repository.domain.ManagePermissionExample;
import cn.hengzhu.main_manager.repository.domain.ManageRoles;
import cn.hengzhu.main_manager.repository.domain.ManageUser;
import cn.hengzhu.main_manager.repository.domain.ManageUserExample;
import cn.hengzhu.main_manager.utils.Md5Util;
import cn.hengzhu.main_manager.utils.ann.SystemControllerLog;
import cn.hengzhu.main_manager.utils.config.gzh.WeiXinConst;


@Controller
public class BinderController {
	
	@Autowired
	private ManageUserMapper manageUserMapper;
	@Autowired
	private ManageRolesMapper manageRolesMapper;
	@Autowired
	private ManagePermissionMapper managePermissionMapper;


	/**
	 * 对微信菜单里面"绑定用户"进行响应
	 */
	@RequestMapping("/binder")
	public String index(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return "binder";
	}

	/**
	 * 当在绑定页面输入"用户名和密码后",点击绑定是会处理
	 */
	@SystemControllerLog(description="用户账号绑定")
	@RequestMapping("/bindingResult")
	public ModelAndView binder(ManageUser manageUser,HttpServletRequest request, HttpServletResponse response) throws Exception {
		String fromUserName = (String) request.getSession().getAttribute("fromUserName");
		if(StringUtils.isNotBlank(fromUserName)){
			String hash = Md5Util.hash(manageUser.getPassWord());
			ManageUserExample example=new ManageUserExample();
			example.createCriteria().andPassWordEqualTo(hash).andFormUserNameEqualTo(manageUser.getLoginName());
			List<ManageUser> selectByExample = manageUserMapper.selectByExample(example);
			// 绑定用户不存在,要跳转到绑定页面,并且给出错误提示信息
			if (selectByExample.isEmpty()) {
				return new ModelAndView("binder", "msg", "用户名或密码不正确!");
			} else {
				ManageUser findByLoginNameAndPassWord = selectByExample.get(0);
				Integer roleId = findByLoginNameAndPassWord.getRoleId();
				ManageRoles manageRoles = manageRolesMapper.selectByPrimaryKey(roleId);
				String permissions = manageRoles.getPermissions();
				List<String> permissionIds = Arrays.asList(permissions.split(","));
				
			    List<Integer> codesInteger = permissionIds.stream().map(Integer::parseInt).collect(Collectors.toList());
				ManagePermissionExample permissionExample=new ManagePermissionExample();
				permissionExample.createCriteria().andIdIn(codesInteger);
				List<ManagePermission> permissionsByIds = managePermissionMapper.selectByExample(permissionExample);
				
				List<String> snList = permissionsByIds.stream().collect(Collectors.mapping(ManagePermission::getSn, Collectors.toList()));
				boolean contains = snList.contains(WeiXinConst.PERMISSION_OPENBOX);
				if(contains){
					try {                          
						findByLoginNameAndPassWord.setFormUserName(fromUserName);
						manageUserMapper.updateByPrimaryKeySelective(findByLoginNameAndPassWord);
						return new ModelAndView("binder", "msg", "绑定成功");
					} catch (Exception e) {
						return new ModelAndView("binder", "msg", "绑定失败，请重试");
					}
				}else{
					return new ModelAndView("binder", "msg", "没有开箱权限不能绑定");
				}
			}
			
		}else{
			return new ModelAndView("binder", "msg", "绑定失败");
		}
	}

}
